package io.github.talelin.latticy.conf;

import com.alibaba.ttl.threadpool.TtlExecutors;
import io.github.talelin.latticy.common.pojo.ThreadConfigEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @authoer:wulongbo
 * @createDate:2022/7/4
 * @description:
 */

@Configuration
public class TranslationThreadPoolConfig {

    @Autowired
    private ThreadConfigEntity threadConfigEntity;

    @Primary
    @Bean(name = "threadPoolTaskExecutor")
    public ExecutorService translationThreadPoolTaskExecutor() {
        return TtlExecutors.getTtlExecutorService(new ThreadPoolExecutor(threadConfigEntity.getCorePoolSize(),
                threadConfigEntity.getMaxPoolSize(), threadConfigEntity.getKeepAliveSeconds(), TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(threadConfigEntity.getQueueCapacity()),
                new ThreadPoolExecutor.CallerRunsPolicy()));
    }
}
